/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.sessionbean;

import cms.dataaccess.DAOFactory;
import dto.Conferenceitem;
import dto.Confitemorder;
import dto.ConfitemorderId;
import dto.Itemauthors;
import dto.ItemauthorsId;
import dto.User;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author TauPo
 */
@Stateless(mappedName="ConferenceItemApplication")
public class ConferenceItemApplication implements ConferenceItemApplicationRemote {

    @Override
    public boolean addConferenceItem(Conferenceitem item) {
      try {
      
            DAOFactory.getInstance().getConferenceItemDAO().insertConferenceItem(item);
             return true;
          
       }
        catch(Exception e){
           System.err.println("Inserting fail +"+item.getId().getItemName()+" ==>"+e.getMessage());
           return false;
        }
     
    }

    @Override
    public boolean deleteConferenceItem(String conferenceName,String itemName) {
      try{
             Conferenceitem delItem =  this.getConferenceItem(conferenceName, itemName);
              DAOFactory.getInstance().getConferenceItemDAO().deleteConferenceItem(delItem);
          
         return true;
      }catch(Exception e ){
         System.err.println("Delete the Conference Item fail item Name["+itemName+"] ==>"+e.getMessage());
         return false;
      }
      
    }

    @Override
    public boolean updateConferenceItem(Conferenceitem item) {
        try{
              DAOFactory.getInstance().getConferenceItemDAO().updateConferenceItem(item);
              return true;
      }catch(Exception e ){
         System.err.println("Update the Conference Item fail +"+item.getId().getItemName()+" ==>"+e.getMessage());
          return false;
      }
    }

    @Override
    public Conferenceitem getConferenceItem(String conferenceName,String itemName) {
        try{
           return (Conferenceitem)( DAOFactory.getInstance().getConferenceItemDAO().
                      findByCustomizeQuery("conferenceName='"+conferenceName+"'"
                   + " and itemName='"+itemName+"'")).get(0);
              
         }catch(Exception e ){
          System.err.println("get the Conference Item fail conference name "+conferenceName+" item name"+ itemName+" ==>"+e.getMessage());
     
          return null;
         }
    }

    @Override
    public List<Conferenceitem> getAllConferenceItems() {
       try{
           return DAOFactory.getInstance().getConferenceItemDAO().findAllConferenceitem();
       }catch(Exception e ){
           System.err.println("get All conference fail==>"+e.getMessage());
           return null;
       }
         
    }

    @Override
    public List<Conferenceitem> getConfercenItems(String itemName) {
        try{
           return  DAOFactory.getInstance().getConferenceItemDAO().
                      findByCustomizeQuery("itemName='"+itemName+"'");
              
         }catch(Exception e ){
          System.err.println("Get the Conference Item fail  item name"+ itemName+" ==>"+e.getMessage());
     
          return null;
         }
    }

    @Override
    public List<Conferenceitem> getConfercenItemsByConferenceName(String conferenceName) {
       try{
           return  DAOFactory.getInstance().getConferenceItemDAO().
                      findByCustomizeQuery("conferenceName='"+conferenceName+"'");
              
         }catch(Exception e ){
          System.err.println("Get the Conference Item fail  conferenceName"+ conferenceName+" ==>"+e.getMessage());
     
          return null;
         }
    }

    @Override
    public List<Itemauthors> getItemAuthors(String itemName) {
        try{
           return  DAOFactory.getInstance().getItemAuthorsDAO().findItemauthorsByItemName(itemName);
         }catch(Exception e ){
          System.err.println("Get the ItemAuthors  itemName"+ itemName+" ==>"+e.getMessage());
     
          return null;
         }
    }
    @Override
    public List<Itemauthors> getItemAuthors(String conferenceName,String itemName){
       
        try{
           return  DAOFactory.getInstance().getItemAuthorsDAO().findByCustomizeQuery("conferenceName='"
                   +conferenceName+"' and itemName='"+itemName+"'");
           
         }catch(Exception e ){
          System.err.println("Get the ItemAuthors  itemName"+ itemName+" ==>"+e.getMessage());
     
          return null;
         }
   }
    @Override
    public Confitemorder bookConferenceItem(String userName, String conferenceName,String itemName) {
        try{
            Conferenceitem  conItem  = DAOFactory.getInstance().getConferenceItemDAO().findConferenceItem(conferenceName, itemName);
            if(conItem == null){
                System.err.println("Conference item ["+conItem.getId().getItemName()+
                        "] is invalidate for conference name " + conItem.getId().getConferenceName());
                return null;
            }
            
            User usr = DAOFactory.getInstance().getUserDAO().findUser(userName);
            if(usr == null){
                System.err.println("User is invalidate for user id "+userName);
                return null;
            }

            
            ConfitemorderId orderid =  new ConfitemorderId(conferenceName,itemName,userName);
            DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
            Date regDate = formatter.parse(currentTime);
           
           
            Confitemorder  itemOrder = new Confitemorder(orderid,conItem.getSellPrice(),0,regDate);
            DAOFactory.getInstance().getConferenceItemOrderDAO().updateConfitemorder(itemOrder);
            DAOFactory.getInstance().getConferenceItemOrderDAO().updateConfitemorder(itemOrder);
            return itemOrder;

          }catch(Exception e){
              System.err.println("Book Conference Item fail ==>"+e.getLocalizedMessage());
              return null;

          }
    }
    @Override
    public boolean unbookConferenceItem(String userName, String conferenceName, String itemName) {
        try{
            Conferenceitem  conItem  = DAOFactory.getInstance().getConferenceItemDAO().findConferenceItem(conferenceName, itemName);
            if(conItem == null){
                System.err.println("Conference item ["+conItem.getId().getItemName()+
                        "] is invalidate for conference name " + conItem.getId().getConferenceName());
                return false;
            }
            
            User usr = DAOFactory.getInstance().getUserDAO().findUser(userName);
            if(usr == null){
                System.err.println("User is invalidate for user id "+userName);
                return false;
            }

            
            ConfitemorderId orderid =  new ConfitemorderId(conferenceName,itemName,userName);
            Confitemorder  itemOrder = new Confitemorder(orderid);
             
            DAOFactory.getInstance().getConferenceItemOrderDAO().deleteConfitemorder(itemOrder);
            return true;

          }catch(Exception e){
              System.err.println("Book Conference Item fail ==>"+e.getLocalizedMessage());
              return false;

          }
    }

    @Override
    public boolean addAuthor(String conferenceName, String itemName, String userName, boolean isFirstAuthor) {
        try {
            Itemauthors author = new Itemauthors();
            ItemauthorsId aid = new ItemauthorsId();
            aid.setConferenceName(conferenceName);
            aid.setItemName(itemName);
            aid.setUserName(userName);
            author.setId(aid);
            author.setIsFirstAuthor(isFirstAuthor);
            DAOFactory.getInstance().getItemAuthorsDAO().insertItemauthors(author);
            return true;
          
       }
        catch(Exception e){
           System.err.println("Error occured when creating author.");
           return false;
        }
    }

   
}
